package com.i2bt.app.friend.model;

import com.i2bt.common.AndroidRestContract;

import com.i2bt.app.BaseActivity;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.friend.FriendAddActivity;
import com.i2bt.bean.FriendBean;

import org.json.JSONException;
import org.json.JSONObject;

public class FriendAddJsonHandle extends BaseJsonModel {

	public FriendAddJsonHandle(BaseActivity activity) {
		super(activity);
	}

	@Override
	protected String logicToWrap() throws JSONException {
		
		switch (beanParam.getAct()) {
		
		case AndroidRestContract.ADD_FRIEND : {
			jsonStrParam = wrapAddFriend();
			break;
		}
		
		}
		
		return jsonStrParam;
	}
	
	private String wrapAddFriend() throws JSONException {
		
		FriendBean friendBean = (FriendBean) beanParam;
		wrapCommonJson();
		jsonParam
		    .put(AndroidRestContract.FRIEND_ID, friendBean.getFriendId())
			.put(AndroidRestContract.FRIEND_MESSAGE_FLAG, friendBean.getFlag())
			.put(AndroidRestContract.FRIEND_MESSAGE_DATE, friendBean.getDate())
			.put(AndroidRestContract.FRIEND_MESSAGE_CONTENT, friendBean.getContent())
			.put(AndroidRestContract.FRIEND_MESSAGE_SENDER, friendBean.getSender())
			.put(AndroidRestContract.FRIEND_MESSAGE_RECEIVER, friendBean.getReceiver())
			.put(AndroidRestContract.FRIEND_MESSAGE_LONGTIME_FLAG, friendBean.getLongTimeFlag());
		
		return jsonParam.toString();
		
	}

	@Override
	protected void logicToUnwrap(boolean isSuc, String result)
			throws JSONException {
		FriendAddActivity friendAddActivity = (FriendAddActivity)activity;
		if (isSuc) {
			
			jsonParam = new JSONObject(result);

			if (jsonParam.getBoolean(AndroidRestContract.SUCCEED)) {
				

				switch (jsonParam.getInt(AndroidRestContract.ACTION)) {
				
				case AndroidRestContract.ADD_FRIEND :  {
					friendAddActivity.toastHandler(jsonParam.
							getString(AndroidRestContract.MESSAGE));
					
				}
				
				}
			}
			else {
				friendAddActivity.toastHandler(jsonParam.
						getString(AndroidRestContract.MESSAGE));
			}
		}
		else {
			friendAddActivity.toastHandler(result);
		}
	}
	
	
	
	
}
